import {service} from '@/utils/request'

// 用户管理API
export interface User {
  id: number
  username: string
  nickname: string
  email: string
  phone: string
  roles: string[]
  permissions: string[]
  created_at: string
  last_login: string
}

export interface UserListParams {
  page: number
  pageSize: number
  keyword?: string
  status?: string
}

export interface UserListResponse {
  records: User[]
  total: number
  page: number
  pageSize: number
}

// 远程治疗方案配置接口
export interface RemoteTreatmentConfig {
  user_id: number
  template_name: string
  template_type: string
  treatment_mode: string
  current_intensity: number
  treatment_duration: number
  frequency?: number
  is_default?: boolean
  remark?: string
}

/**
 * 获取用户列表
 */
export const getUserList = (params: UserListParams) => {
  // 确保参数名称与后端一致
  const queryParams = {
    page: params.page,
    per_page: params.pageSize,  // 后端使用 per_page 而不是 pageSize
    keyword: params.keyword || '',
    status: params.status || ''
  }
  
  return service<UserListResponse>({
    url: '/users',
    method: 'get',
    params: queryParams
  })
}

/**
 * 获取用户详情
 */
export const getUserDetail = (id: string) => {
  return service<User>({
    url: `/users/${id}`,
    method: 'get'
  })
}

/**
 * 创建用户
 */
export const createUser = (data: Partial<User>) => {
  return service<User>({
    url: '/users',
    method: 'post',
    data
  })
}

/**
 * 更新用户
 */
export const updateUser = (id: number, data: Partial<User>) => {
  return service<User>({
    url: `/users/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除用户
 */
export const deleteUser = (id: number) => {
  return service({
    url: `/users/${id}`,
    method: 'delete'
  })
}

/**
 * 更新用户信息
 */
export const updateUserInfo = (id: number, data: Partial<User>) => {
  return service<User>({
    url: `/users/${id}`,
    method: 'put',
    data
  })
}

/**
 * 修改密码
 */
export const changePassword = (data: { old_password: string; new_password: string }) => {
  return service({
    url: '/auth/change-password',
    method: 'post',
    data
  })
}

/**
 * 为用户远程配置治疗方案
 */
export const createRemoteTreatmentTemplate = (data: RemoteTreatmentConfig) => {
  return service({
    url: '/treatments/templates',
    method: 'post',
    data
  })
}